package com.xmu.makerspace.service;

import com.xmu.makerspace.dao.*;
import com.xmu.makerspace.domain.FormalMember;
import com.xmu.makerspace.domain.FormalTeam;
import com.xmu.makerspace.domain.MiddleTermResult;
import com.xmu.makerspace.model.Team.MemberMessage;
import com.xmu.makerspace.model.Team.TeamListVO;
import com.xmu.makerspace.model.Team.TeamMessage;
import com.xmu.makerspace.model.Team.TeamMore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class TeamManagementService {
    @Autowired
    MesNumRepository mesNumRepository;
    @Autowired
    FormalMemberRepository formalMemberRepository;
    @Autowired
    SeatRepository seatRepository;
    @Autowired
    StudentRepository studentRepository;
         private FormalTeamRepository formalTeamRepository;
         private MiddleTermResultRepository middleTermResultRepository;
         private MiddleTermResult middleTermResult;
         public FormalTeam formalTeam;
         public TeamManagementService(FormalTeamRepository formalTeamRepository,MiddleTermResultRepository middleTermResultRepository)
         {
             this.formalTeamRepository=formalTeamRepository;
             this.middleTermResultRepository=middleTermResultRepository;
         }
    //获取团队进程
         public int findTeamById(int teamId)
         {
        formalTeam=this.formalTeamRepository.findAllByTeamId(teamId);
        return formalTeam.getAuditStep();
         }

    public MiddleTermResult getMiddleTermResult(int teamId)//获取团队中期评价
        {
        middleTermResult=this.middleTermResultRepository.findAllByTeamId(teamId);
        return middleTermResult;
        }
        /*
        @method:getTeamList
        @param:获取正式团队的列表
         */
        public TeamListVO GetTeamList(int page)
        {
            TeamListVO teamListVO=new TeamListVO();
            int num=10;//一页的最多团队个数
            ArrayList<FormalTeam> formalTeams=formalTeamRepository.findAll();
            int all=formalTeams.size();
            ArrayList<FormalTeam> formalTeamsend=new ArrayList<>(num);
            if(num*page>all)
            {
                for(int i=num*(page-1);i<all;i++)
                    formalTeamsend.add(formalTeams.get(i));
                teamListVO.setPart(all-num*(page-1));
            }
            else
            {
                for(int i=num*(page-1);i<num*page;i++)
                    formalTeamsend.add(formalTeams.get(i));
                teamListVO.setPart(num);
            }
            teamListVO.setAll(all);
            ArrayList<TeamMessage> teamMessages=new ArrayList<>(teamListVO.getPart());
            for(int i=0;i<teamListVO.getPart();i++)
            {
                TeamMessage teamMessage=new TeamMessage();
                teamMessage.setBelong_field(formalTeamsend.get(i).getBelongField());
                teamMessage.setProject_name(formalTeamsend.get(i).getProjectName());
                teamMessage.setProject_type(formalTeamsend.get(i).getProjectType());
                teamMessage.setRegister_date(formalTeamsend.get(i).getRegisterDate());
                teamMessage.setTeam_name(formalTeamsend.get(i).getTeamName());
                teamMessage.setTeamId(formalTeamsend.get(i).getTeamId());
                teamMessage.setMessage(mesNumRepository.findAllByTeamId(teamMessage.getTeamId()).getMessageNum());
                teamMessages.add(teamMessage);
            }
            teamListVO.setFormalTeams(teamMessages);
            return teamListVO;
        }
        /*
        @method:getTeamDetail
        获取团队详细信息
         */
        public TeamMore getTeamDetail(int team_id)
        {
            TeamMore teamMore=new TeamMore();
            FormalTeam formalTeam=formalTeamRepository.findAllByTeamId(team_id);
            teamMore.setBlong_field(formalTeam.getBelongField());
            teamMore.setWork_foundation(formalTeam.getWorkFoundation());
            teamMore.setTeam_name(formalTeam.getTeamName());
            teamMore.setTeam_id(formalTeam.getTeamId());
            teamMore.setRegister_date(formalTeam.getRegisterDate());
            teamMore.setProject_type(formalTeam.getProjectType());
            teamMore.setProject_director(formalTeam.getProjectDirector());
            teamMore.setProject_brief(formalTeam.getProjectBrief());
            teamMore.setPlan(formalTeam.getPlan());
            ArrayList<FormalMember> formalMembers=formalMemberRepository.findByTeamId(team_id);
            ArrayList<MemberMessage> memberMessages=new ArrayList<>(formalMembers.size());
            for(int i=0;i<formalMembers.size();i++)
            {
                MemberMessage memberMessage=new MemberMessage();
                memberMessage.setStudent_id(formalMembers.get(i).getStudentId());
                memberMessage.setStudent_name(formalMembers.get(i).getStudentName());
                memberMessage.setSeat_no(seatRepository.findByStudentId(memberMessage.getStudent_id()).getSeatNo());
                memberMessage.setRoom_no(seatRepository.findByStudentId(memberMessage.getStudent_id()).getRoomNo());
                memberMessage.setAttendance_number(studentRepository.findOne(memberMessage.getStudent_id()).getAttendanceNumber());
                memberMessages.add(memberMessage);
            }
            teamMore.setMemberMessages(memberMessages);
            return teamMore;
        }
}
